/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asja.formulario;

import asja.bd.Conexao;
import asja.modelo.NotasModelo;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alice
 */
public class VisualizarNotasFormulario extends JFrame{
    
     private Connection conecta;
     private JTable tabela, tabela_nome;
      private JPanel painel_tabela;
      private DefaultTableModel modelo, modelo_nome; // depois instanciar
     private JScrollPane js_tabela, js_tabela_nome;
      private JLabel jlbl_img;
      private JButton bt_imprimir,bt_cancelar, bt_pesquisar, bt_eliminar, bt_editar;
      private JTextField jtf_pesquisar, jtf_rg;
  //    List <FuncionarioModelo> nomes;
    
    
    

    public VisualizarNotasFormulario() throws SQLException  {
        
        this.conecta = Conexao.getConnection();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setTitle("Visualizar Notas");  
        this.setSize(700, 500);          
        this.setLocationRelativeTo(null);  
    }
    
    public void CriaJanela(){
        
                
       // String espaco= new String(" ");
        modelo = new DefaultTableModel(); 
        modelo_nome = new DefaultTableModel();        
        tabela= new JTable(modelo);
        tabela_nome= new JTable(modelo_nome);
        jlbl_img= new JLabel();
        bt_imprimir= new JButton("Imprimir");
        bt_pesquisar= new JButton("Pesquisar");
        bt_cancelar= new JButton("Cancelar");
        bt_eliminar= new JButton("Eliminar");
        bt_editar= new JButton("Editar");
        js_tabela= new JScrollPane(tabela);
        js_tabela_nome= new JScrollPane(tabela_nome);    
        jtf_pesquisar= new JTextField("", 50);
        jtf_rg= new JTextField("", 20);
        
        
        this.cria_tabela();   
        
        //Painel cabecalho
       JPanel painel_cabecalho = new JPanel();         
       painel_cabecalho.setLayout(new BorderLayout());            
       jlbl_img.setIcon(new ImageIcon("imagens\\visualiza.jpg"));
       painel_cabecalho.add(jlbl_img, BorderLayout.CENTER); 
       
       //Painel pesquisa
       JPanel painel_pesquisa = new JPanel();
       painel_pesquisa.setLayout(new FlowLayout());
       painel_pesquisa.add(jtf_pesquisar);
       painel_pesquisa.add(bt_pesquisar);
       painel_pesquisa.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Pesquisa"));
       
       
    
       //Painel tabela
       painel_tabela = new JPanel();
       painel_tabela.setLayout(new BorderLayout());
       painel_tabela.add(painel_pesquisa, BorderLayout.NORTH);
       painel_tabela.add(js_tabela, BorderLayout.CENTER);
       
    
       
       //painel para os comandos
       JPanel painel_comandos = new JPanel();         
       painel_comandos.setLayout(new FlowLayout()); 
       painel_comandos.add(bt_imprimir);
       painel_comandos.add(bt_editar);
       painel_comandos.add(bt_eliminar);
       painel_comandos.add(bt_cancelar);
       

        //criando o painel principal e adicionando ao JFrame
        JPanel painel_geral = new JPanel();
        painel_geral.setLayout(new BorderLayout());
        painel_geral.add(painel_cabecalho, BorderLayout.NORTH);
        
        painel_geral.add(painel_tabela, BorderLayout.CENTER);
        painel_geral.add(painel_comandos, BorderLayout.SOUTH);
        this.getContentPane().add(painel_geral);  
        
         bt_cancelar.addMouseListener(new MouseAdapter() {
        @Override
            public void mouseClicked(MouseEvent evt){
                MenuPrincipalFormulario mnu_principal = new MenuPrincipalFormulario(); 
                mnu_principal.criajanela_menuprincipal();  
                mnu_principal.aparencia_MenuPrincipal();
                dispose();
                                 
            }});
         
         bt_pesquisar.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent evt){
             try {
                    compara();
                } catch (SQLException ex) {
                    Logger.getLogger(VisualizarNotasFormulario.class.getName()).log(Level.SEVERE, null, ex);
                }
                                 
            }});
         
        this.setVisible(true);
        this.setResizable(false);
        
        
        
        
    }
    
    
    
    
    private void cria_tabela(){
        
        js_tabela.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
        tabela.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        js_tabela.setHorizontalScrollBar(new JScrollBar(0));
        
        
         //adicionando colunas a tabela
         modelo.addColumn("Nome Completo ");
         modelo.addColumn("Frequencia");
         modelo.addColumn("Nota1");
         modelo.addColumn("Nota2");
         modelo.addColumn("Nota3");
         modelo.addColumn("Nota4");
         modelo.addColumn("Nota4");
       
         //redimensionando as colunas
         tabela.getColumnModel().getColumn(0).setPreferredWidth(200);
         tabela.getColumnModel().getColumn(1).setPreferredWidth(100);
         tabela.getColumnModel().getColumn(2).setPreferredWidth(100);
         tabela.getColumnModel().getColumn(3).setPreferredWidth(120);
         tabela.getColumnModel().getColumn(4).setPreferredWidth(100);
         tabela.getColumnModel().getColumn(5).setPreferredWidth(100);
         tabela.getColumnModel().getColumn(6).setPreferredWidth(100);
      
    }
    
     public void visualizaNotas () throws SQLException
   {
		
		ResultSet rs;            
                
                PreparedStatement stmt = this.conecta.prepareStatement ("select * from notas order by nome");
		rs = (ResultSet) stmt.executeQuery();	
                
               while (rs.next()){
                    
                    NotasModelo funcionario = new NotasModelo();
                    modelo.addRow(new String []{rs.getString("nome"),rs.getString("frequencia"), rs.getString("nota1"), rs.getString("nota2"), rs.getString("nota3"),rs.getString("nota4") , rs.getString("nota5")});
		}
		stmt.close();
		conecta.close();
   } 
     
  public int pesquisa_notas() throws SQLException{
        
       conecta = Conexao.getConnection();
       PreparedStatement stmt = conecta.prepareStatement("select * from notas where nome like ?");                   
       stmt.setString(1, jtf_pesquisar.getText());// consulta por nome
      
      
       ResultSet rs = stmt.executeQuery(); //Para consultas, retorna um objeto ResultSet    
      int i =0;// variável auxiliar que vai armazenar quantos registros resultam de uma determinada consulta
       
       while(modelo.getRowCount()>0){
         modelo.removeRow(0);
       }
      while (rs.next()) {
               modelo.addRow(new String []{rs.getString("nome"),rs.getString("frequencia"), rs.getString("nota1"), rs.getString("nota2"), rs.getString("nota3"),rs.getString("nota4") , rs.getString("nota5")});
               i++;
        }
        stmt.close();
        conecta.close();
        return i;
   
    }
  
  
  public void compara()throws SQLException{ 
         
         int i=pesquisa_notas();
        if(i==0){
          JOptionPane.showMessageDialog(VisualizarNotasFormulario.this, "Aluno inexistente", "Pesquisar Nota", JOptionPane.WARNING_MESSAGE);
          
        }else
           i=pesquisa_notas();
   }
  /*
   public static void main(String [] args){
          
          
        try {
            VisualizarNotasFormulario mnu_visualiza; 
            mnu_visualiza = new VisualizarNotasFormulario();
            mnu_visualiza.CriaJanela();
            mnu_visualiza.visualizaNotas();
        } catch (SQLException ex) {
            Logger.getLogger(VisualizaFuncionarioFormulario.class.getName()).log(Level.SEVERE, null, ex);
        }
          
   }*/
     
      
}
